<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">年度</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input"
                                       id="searchYear" name="searchYear"
                                       placeholder="请选择年度"
                                       autocomplete="off" >
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"
                                    lay-filter="data-search-btn" id="data-search-btn"
                                    lay-submit=""><i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary"
                                    lay-filter="data-reset-btn" id="data-reset-btn">
                                <i class="layui-icon"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加用户</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="removeBatch"> 批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="remove">删除</a>
        </script>

        <script type="text/html" id="checkStatusTable">
            <input type="checkbox" checked lay-skin="switch"
                   lay-text="启用|禁用">
        </script>

        <!--        <script type="text/html" id="usernameTmp">-->
        <!--                &lt;!&ndash; href改为用户详情页面userDetail.jsp&ndash;&gt;-->
        <!--                <a href="table/userDetail.jsp" class="layui-table-link">{{d.username}}</a>-->
        <!--         </script>-->

    </div>
</div>

<script src="lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="lib/jq-module/paigusu.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','miniPage','element','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            laydate = layui.laydate,
            miniPage = layui.miniPage,
            initYear;

        form.render();

        let tableObj = table.render({
            elem: '#currentTableId',
            url: 'CompetitionServlet?method=search',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'category', width: 120, title: '竞赛类别'},
                {field: 'year', width: 120, title: '年度'},
                {field: 'title', width: 180, title: '题目'},
                {field: 'leader', width: 120, title: '组长'},
                {field: 'members', width: 150, title: '组员'},
                {field: 'level', width:120, title: '获奖等级'},
                {field: 'teacher', width: 120, title: '指导老师'},
                {title: '操作', minWidth: 130, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        laydate.render({
            elem: '#searchYear',
            type: 'year',
            trigger: 'click',
            ready: function (date) { // 控件在打开时触发，回调返回一个参数：初始的日期时间对象
                initYear = date.year;
            },
            change: function (value, date, endDate) { // 年月日时间被切换时都会触发。回调返回三个参数，分别代表：生成的值、日期时间对象、结束的日期时间对象
                var selectYear = date.year;
                var differ = selectYear - initYear;
                if (differ != 15 && differ != -15) {
                    if ($(".layui-laydate").length) {
                        $("#searchYear").val(value);
                        $(".layui-laydate").remove();
                    }
                }
                initYear = selectYear;
            }
        })

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var content = miniPage.getHrefContent('page/competition/competitionAdd.jsp');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加参赛信息',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'removeBatch') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;

                layer.confirm("确认删除这些行吗？",function (index) {
                    $.ajaxSettings.async = false;
                    let successNumber = 0;
                    let failNumber = 0;
                    for(let i=0;i<data.length;i++){
                        let url = 'CompetitionServlet';
                        let params = {method:'remove', id: data[i].id};
                        let successFn = function (res) {
                            if (res == 'true') {
                                successNumber++;
                            }else{
                                failNumber++;
                            }
                        }

                        $.post(url, params, successFn);
                    }
                    $.ajaxSettings.async = true;
                    layer.msg("["+successNumber+"]条信息删除成功  /  ["+failNumber+"]条信息删除失败");
                    layer.close(index);
                    table.reload('currentTableId');

                });

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                let url = 'CompetitionServlet';
                let params = {method:'searchById', id: data.id};
                let successFn = function (res) {
                    res = JSON.parse(res);
                    let params = "?id="+res.id;
                    params+="&category="+res.category;
                    params+="&year="+res.year;
                    params+="&title="+res.title;
                    params+="&leader="+res.leader;
                    params+="&members="+res.members;
                    params+="&level="+res.level;
                    params+="&teacher="+res.teacher;

                    var content = miniPage.getHrefContent('page/competition/competitionModify.jsp'+params);
                    var openWH = miniPage.getOpenWidthHeight();

                    var index = layer.open({
                        title: '修改参赛信息',
                        type: 1,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: [openWH[0] + 'px', openWH[1] + 'px'],
                        offset: [openWH[2] + 'px', openWH[3] + 'px'],
                        content: content,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                }

                $.get(url, params, successFn);

            } else if (obj.event === 'remove') {
                layer.confirm('真的删除行么', function (index) {
                    let url = 'CompetitionServlet';
                    let params = {method:'remove', id: data.id};
                    let successFn = function (res) {
                        if (res == 'true') {
                            obj.del();
                            layer.msg("删除成功",{icon: 1});
                        }else{
                            layer.msg("删除失败",{icon: 2});
                        }
                    }
                    $.post(url, params, successFn);
                    layer.close(index);
                });
            }
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let params = {
                page:{curr:1,limit: 10},
                where:data.field
            }
            tableObj.reload(params);
        });

        $('#data-reset-btn').click(function () {
            let params = {
                page:{curr:1,limit: 10},
                where:null
            }
            tableObj.reload(params);
        })

    });
</script>